/******************************************************************************
*@file  : app.c
*@brief : AES_ROM Demos 
******************************************************************************/
#include "app.h"

UART_HandleTypeDef  UART1_Handle;
UART_HandleTypeDef  UART2_Handle;


#define SM3_SWAP32(a)       ((a<<24)|((a&0x0000ff00)<<8)|((a&0x00ff0000)>>8)|(a>>24))

void swap(uint32_t *indata,uint32_t len)
{
	uint32_t i;
	for(i = 0; i < len; i ++)
	{
		indata[i] = SM3_SWAP32(indata[i]);	
	}
}

uint32_t aes_ecb_test(void)  //ECB Test
{    
	uint32_t i;
	uint32_t result[8];
	uint32_t blk_len;
	//key_ecb_128    = 0xe04ad1a4 65fe706b 80cf2960 5da2636a
	uint32_t key_ecb_128[4]     = {0xe04ad1a4, 0x65fe706b, 0x80cf2960, 0x5da2636a};
	//iv_ecb_128     = 0x0	
	uint32_t iv_ecb_128[4]      = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
	//plain_ecb_128  = 0xc42821e1 3ac9b622 72304ad5 e5f5937d	
	uint32_t plain_ecb_128[4]   = {0xc42821e1, 0x3ac9b622, 0x72304ad5, 0xe5f5937d};
	//cipher_ecb_128 = 0x1f4f49e4 fb96cca5 a309032f b1a46dde	
	uint32_t cipher_ecb_128[4]  = {0x1f4f49e4, 0xfb96cca5, 0xa309032f, 0xb1a46dde};  

	//key_ecb_192    = 0xe04ad1a4 65fe706b 80cf2960 5da2636a c42821e1 3ac9b622
	uint32_t key_ecb_192[6]     = {0xe04ad1a4, 0x65fe706b, 0x80cf2960, 0x5da2636a, 0xc42821e1, 0x3ac9b622};
	//iv_ecb_192     = 0x0
	uint32_t iv_ecb_192[4]      = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
	//plain_ecb_192  = 0x72304ad5 e5f5937d f38d8adf 367b0c53	
	uint32_t plain_ecb_192[4]   = {0x72304ad5, 0xe5f5937d, 0xf38d8adf, 0x367b0c53};
	//cipher_ecb_192 = 0xc8f26de1 68a21c9d 21323441 9e11db4e	
	uint32_t cipher_ecb_192[4]  = {0xc8f26de1, 0x68a21c9d, 0x21323441, 0x9e11db4e};  

	//key_ecb    = 0xe04ad1a4 65fe706b 80cf2960 5da2636a c42821e1 3ac9b622 72304ad5 e5f5937d
	uint32_t key_ecb_256[8]     = {0xe04ad1a4, 0x65fe706b, 0x80cf2960, 0x5da2636a, 0xc42821e1, 0x3ac9b622, 0x72304ad5, 0xe5f5937d}; 
	//iv_ecb     = 0x0	
	uint32_t iv_ecb_256[4]      = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
	//plain_ecb  = 0xf38d8adf 367b0c53 1d273eb7 60632039	
	uint32_t plain_ecb_256[4]   = {0xf38d8adf, 0x367b0c53, 0x1d273eb7, 0x60632039};
	//cipher_ecb = 0xaddf70c6 3034834d 5c3ad484 ebbf7ed0	
	uint32_t cipher_ecb_256[4]  = {0xaddf70c6, 0x3034834d, 0x5c3ad484, 0xebbf7ed0};  


    //-----------------------    ECB mode (no swap)   ---------------------
//128
	blk_len = sizeof(cipher_ecb_128) / 16;
    HAL_AES_Crypt_ECB_rom(key_ecb_128, AES_KEY_128, AES_SWAP_DISABLE, plain_ecb_128, result, blk_len, AES_ENCRYPTION);

	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != cipher_ecb_128[i])
		{
			return 1;
		}
	}    
    printf("AES-128 ECB encrypt pass\n");
 
    HAL_AES_Crypt_ECB_rom(key_ecb_128, AES_KEY_128, AES_SWAP_DISABLE, cipher_ecb_128, result, blk_len, AES_DECRYPTION);
	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != plain_ecb_128[i])
		{
			return 1;
		}
	}
    printf("AES-128 ECB decrypt pass\n");

//192
	blk_len = sizeof(plain_ecb_192) / 16;
    HAL_AES_Crypt_ECB_rom(key_ecb_192, AES_KEY_192, AES_SWAP_DISABLE, plain_ecb_192, result, blk_len, AES_ENCRYPTION);

	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != cipher_ecb_192[i])
		{
			return 1;
		}
	} 
    printf("AES-192 ECB encrypt pass\n");
	
    HAL_AES_Crypt_ECB_rom(key_ecb_192, AES_KEY_192, AES_SWAP_DISABLE, cipher_ecb_192, result, blk_len, AES_DECRYPTION);

	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != plain_ecb_192[i])
		{
			return 1;
		}
	}
    printf("AES-192 ECB decrypt pass\n");

//256
	blk_len = sizeof(plain_ecb_256) / 16;
    HAL_AES_Crypt_ECB_rom(key_ecb_256, AES_KEY_256, AES_SWAP_DISABLE, plain_ecb_256, result, blk_len, AES_ENCRYPTION);

	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != cipher_ecb_256[i])
		{
			return 1;
		}
	}    
    printf("AES-256 ECB encrypt pass\n");

    HAL_AES_Crypt_ECB_rom(key_ecb_256, AES_KEY_256, AES_SWAP_DISABLE, cipher_ecb_256, result, blk_len, AES_DECRYPTION);
	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != plain_ecb_256[i])
		{
			return 1;
		}
	}
    printf("AES-256 ECB decrypt pass\n");

	//-----------------------   ECB mode (swap)   ---------------------
	blk_len = sizeof(plain_ecb_128) / 16;
																			 
	swap(key_ecb_128,4);            //key_ecb_128[0] = 0xa4d14ae0,...,key_ecb_128[3] = 0x6a63a25d
	swap(iv_ecb_128,4);
	swap(plain_ecb_128,blk_len*4);
	swap(cipher_ecb_128,blk_len*4);

    HAL_AES_Crypt_ECB_rom(key_ecb_128, AES_KEY_128, AES_SWAP_ENABLE, plain_ecb_128, result, blk_len, AES_ENCRYPTION);

	for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != cipher_ecb_128[i])
		{
			return 1;
		}
	}    
    printf("AES-128 ECB(swap) encrypt pass\n");

    HAL_AES_Crypt_ECB_rom(key_ecb_128, AES_KEY_128, AES_SWAP_ENABLE, cipher_ecb_128, result, blk_len, AES_DECRYPTION);
    
    for( i = 0; i < (blk_len*4); i ++)
	{
		if(result[i] != plain_ecb_128[i])
		{
			return 1;
		}
	}
    printf("AES-128 ECB(swap) decrypt pass\n");
    
	//-----------------------   End of  ECB mode   ---------------------
		return 0;
}

uint32_t aes_cbc_test(void)   //CBC Test
{
    uint32_t i;
    uint32_t result[20];
    uint32_t blk_len;
//128	
    //key_cbc_128    = 0xe04ad1a4 65fe706b 80cf2960 5da2636a
    uint32_t key_cbc_128[4]     = {0xe04ad1a4, 0x65fe706b, 0x80cf2960, 0x5da2636a};
    //iv_cbc_128     = 0xc42821e1 3ac9b622 72304ad5 e5f5937d
    uint32_t iv_cbc_128[4]      = {0xc42821e1, 0x3ac9b622, 0x72304ad5, 0xe5f5937d};    
    //plain_cbc_128  = 0xf38d8adf 367b0c53 1d273eb7 60632039...
    uint32_t plain_cbc_128[20]  = {0xf38d8adf, 0x367b0c53, 0x1d273eb7, 0x60632039, 
                                 0x7e314c2f, 0xa1ab5e18, 0x79aa587e, 0xb834e5de, 
                                 0x51d45354, 0xafcebfd6, 0x7a9cd97c, 0x5be67590, 
                                 0x40f27cfd, 0xc60d77f7, 0xf923668d, 0xb2fde000, 
                                 0x20020634, 0x3fee8224, 0x097ed2f3, 0x4f872281}; 
    //cipher_cbc_128 = 0x099ed143 4af12194 c2cb0351 075ea970...
    uint32_t cipher_cbc_128[20] = {0x099ed143, 0x4af12194, 0xc2cb0351, 0x075ea970, 
                                 0x59c1e350, 0x9c5bcc75, 0x34503402, 0xe1ad79a9, 
                                 0xae2bd3d2, 0x4ec9e23d, 0x35162f55, 0x993a1b83, 
                                 0x35779406, 0x00d30e40, 0xc28639a1, 0x4ea39e14, 
                                 0x5a94dd9d, 0x80adf249, 0x863da72a, 0x71021e78};  
//192
    //key_cbc_192    = 0xe04ad1a4 65fe706b 80cf2960 5da2636a c42821e1 3ac9b622
    uint32_t key_cbc_192[6]     = {0xe04ad1a4, 0x65fe706b, 0x80cf2960, 0x5da2636a, 0xc42821e1, 0x3ac9b622}; 
    //iv_cbc_192    = 0x72304ad5 e5f5937d f38d8adf 367b0c53
    uint32_t iv_cbc_192[4]      = {0x72304ad5, 0xe5f5937d, 0xf38d8adf, 0x367b0c53}; 
    //plain_cbc_192  = 0x1d273eb7 60632039 7e314c2f a1ab5e18...
    uint32_t plain_cbc_192[20]  = {0x1d273eb7, 0x60632039, 0x7e314c2f, 0xa1ab5e18, 
                                 0x79aa587e, 0xb834e5de, 0x51d45354, 0xafcebfd6, 
                                 0x7a9cd97c, 0x5be67590, 0x40f27cfd, 0xc60d77f7, 
                                 0xf923668d, 0xb2fde000, 0x20020634, 0x3fee8224, 
                                 0x097ed2f3, 0x4f872281, 0x4329f38c, 0x6c564821};
    //cipher_cbc_192 = 0x4c9abdee 35097f17 37de6468 0a14af58...
    uint32_t cipher_cbc_192[20] = {0x4c9abdee, 0x35097f17, 0x37de6468, 0x0a14af58, 
                                 0x20e48235, 0x31a46420, 0x93a283f4, 0x7e982a45, 
                                 0x9e15fd81, 0xf80f2328, 0x0701d24d, 0x4c7bcb79, 
                                 0xbcfd3d10, 0xdc3dceb2, 0x800fa9d1, 0xadc513f9, 
                                 0xfa161563, 0x3c65e394, 0xbad169b6, 0xac2f04cd}; 

//256
    //key_cbc_256    = 0xe04ad1a4 65fe706b 80cf2960 5da2636a c42821e1 3ac9b622 72304ad5 e5f5937d
    uint32_t key_cbc_256[8]     = {0xe04ad1a4, 0x65fe706b, 0x80cf2960, 0x5da2636a, 0xc42821e1, 0x3ac9b622, 0x72304ad5, 0xe5f5937d}; 
    //iv_cbc_256    = 0xf38d8adf 367b0c53 1d273eb7 60632039
    uint32_t iv_cbc_256[4]      = {0xf38d8adf, 0x367b0c53, 0x1d273eb7, 0x60632039}; 
    // plain_cbc_256  = 0x7e314c2f a1ab5e18 79aa587e b834e5de...		
    uint32_t plain_cbc_256[20]  = {0x7e314c2f, 0xa1ab5e18, 0x79aa587e, 0xb834e5de,
                                 0x51d45354, 0xafcebfd6, 0x7a9cd97c, 0x5be67590, 
                                 0x40f27cfd, 0xc60d77f7, 0xf923668d, 0xb2fde000,
                                 0x20020634, 0x3fee8224, 0x097ed2f3, 0x4f872281,
                                 0x4329f38c, 0x6c564821, 0x6e897e46, 0x25ac03f3}; 
    //cipher_cbc_256 = 0xc8324f39 cf6969a7 35f97dd9 34a90318...		
    uint32_t cipher_cbc_256[20] = {0xc8324f39, 0xcf6969a7, 0x35f97dd9, 0x34a90318,
                                 0x72866a00, 0x2fd8e17d, 0xf8003085, 0x4e796949,
                                 0xcd70af28, 0xe13be0d2, 0x2d613def, 0x8da350f6,
                                 0x7107db84, 0x1c3f5585, 0xc77b89dc, 0x4ccd5abc,
                                 0x307463b8, 0x5098659e, 0xdd3c2694, 0xedd32cdb};  


    //-----------------------    CBC mode (no swap)   ---------------------
    //128
    blk_len = sizeof(plain_cbc_128) / 16;       
    HAL_AES_Crypt_CBC_rom(key_cbc_128, AES_KEY_128, AES_SWAP_DISABLE, plain_cbc_128, iv_cbc_128, result, blk_len, AES_ENCRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != cipher_cbc_128[i])
        {
            return 1;
        }
    }
    printf("AES-128 CBC encrypt pass\n");

   HAL_AES_Crypt_CBC_rom(key_cbc_128, AES_KEY_128, AES_SWAP_DISABLE, cipher_cbc_128, iv_cbc_128, result, blk_len, AES_DECRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != plain_cbc_128[i])
        {
            return 1;
        }
    }
    printf("AES-128 CBC decrypt pass\n");

    //192
    blk_len = sizeof(plain_cbc_192) / 16;        
    HAL_AES_Crypt_CBC_rom(key_cbc_192, AES_KEY_192, AES_SWAP_DISABLE, plain_cbc_192, iv_cbc_192, result, blk_len, AES_ENCRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != cipher_cbc_192[i])
        {
            return 1;
        }
    }
    printf("AES-192 CBC encrypt pass\n");

    HAL_AES_Crypt_CBC_rom(key_cbc_192, AES_KEY_192, AES_SWAP_DISABLE, cipher_cbc_192, iv_cbc_192, result, blk_len, AES_DECRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != plain_cbc_192[i])
        {
            return 1;
        }
    }
    printf("AES-192 CBC decrypt pass\n");

    //256
    blk_len = sizeof(plain_cbc_256) / 16;
        
   HAL_AES_Crypt_CBC_rom(key_cbc_256, AES_KEY_256, AES_SWAP_DISABLE, plain_cbc_256, iv_cbc_256, result, blk_len, AES_ENCRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != cipher_cbc_256[i])
        {
            return 1;
        }
    }
    printf("AES-256 CBC encrypt pass\n");

   HAL_AES_Crypt_CBC_rom(key_cbc_256, AES_KEY_256, AES_SWAP_DISABLE, cipher_cbc_256, iv_cbc_256, result, blk_len, AES_DECRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != plain_cbc_256[i])
        {
            return 1;
        }
    }
    printf("AES-256 CBC decrypt pass\n");

    //-----------------------    CBC mode (swap)   ---------------------                                       
    blk_len = sizeof(plain_cbc_128) / 16;

    swap(key_cbc_128,4);            //key_cbc_128[0] = 0xa4d14ae0,...,key_cbc_128[3] = 0x6a63a25d
    swap(iv_cbc_128,4);
    swap(plain_cbc_128,blk_len*4);
    swap(cipher_cbc_128,blk_len*4);	

    HAL_AES_Crypt_CBC_rom(key_cbc_128, AES_KEY_128, AES_SWAP_ENABLE, plain_cbc_128, iv_cbc_128, result, blk_len, AES_ENCRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != cipher_cbc_128[i])
        {
            return 1;
        }
    }
    printf("AES-128 CBC(swap) encrypt pass\n");

   HAL_AES_Crypt_CBC_rom(key_cbc_128, AES_KEY_128, AES_SWAP_ENABLE, cipher_cbc_128, iv_cbc_128, result, blk_len, AES_DECRYPTION);

    for( i = 0; i < (blk_len*4); i ++)
    {
        if(result[i] != plain_cbc_128[i])
        {
            return 1;
        }
    }        
    printf("AES-128 CBC(swap) decrypt pass\n");

    //-----------------------   End of  CBC mode   ---------------------
    return 0;
}

/******************************************************************************
*@brief : APP_AES_Rom_Test
*@param : none
*@return: none
******************************************************************************/
void APP_AES_Rom_Test(void)
{
	if(aes_ecb_test())
		printfS(" AES ECB Test Fail!\r\n");
	else
		printfS(" AES ECB Test Pass!\r\n\n"); 
	if(aes_cbc_test())
		printfS(" AES CBC Test Fail!\r\n");
	else
		printfS(" AES CBC Test Pass!\r\n\n");
    
	printfS("AES Test Over!\r\n");    
}
